package com.sgh.springsecurityoauth2.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 认证服务配置
 * @Author: sgh
 * @Date: 2025/3/2
 */
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private UserDetailsService userDetailsService;

    // @Autowired
    // @Qualifier("redisTokenStore")
    // private TokenStore redisTokenStore;

    @Autowired
    @Qualifier("jwtTokenStore")
    private TokenStore jwtTokenStore;
    @Autowired
    @Qualifier("jwtAccessTokenConverter")
    private JwtAccessTokenConverter jwtAccessTokenConverter;
    @Autowired
    @Qualifier("jwtTokenEnhancer")
    private TokenEnhancer jwtTokenEnhancer;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                // 客户端id
                .withClient("client")
                // 客户端密码
                .secret(passwordEncoder.encode("112233"))
                //重定向地址
                // .redirectUris("http://www.baidu.com")
                .redirectUris("http://127.0.0.1:8081/login")
                // 授权范围
                .scopes("all")
                .autoApprove(true)
                // token有效期
                .accessTokenValiditySeconds(30)
                /**
                 * 授权类型
                 * authorization_code: 授权码模式
                 * password: 密码模式
                 */
                .authorizedGrantTypes("authorization_code", "password")
                // client82
                .and()
                .withClient("client82")
                .secret(passwordEncoder.encode("112233"))
                .redirectUris("http://127.0.0.1:8082/login")
                .authorizedGrantTypes("authorization_code")
                .autoApprove(true)
                .scopes("all")
                .and()
                // client83
                .withClient("client83")
                .secret(passwordEncoder.encode("112233"))
                .redirectUris("http://127.0.0.1:8083/login")
                .authorizedGrantTypes("authorization_code")
                .autoApprove(true)
                .scopes("all");
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        // 单点登录配置
        security.tokenKeyAccess("isAuthenticated()");
    }

    /**
     * 密码模式配置
     * @param endpoints
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

        // token增强器
        TokenEnhancerChain chain = new TokenEnhancerChain();
        List<TokenEnhancer> tokenEnhancers = new ArrayList<>();
        tokenEnhancers.add(jwtTokenEnhancer);
        tokenEnhancers.add(jwtAccessTokenConverter);
        chain.setTokenEnhancers(tokenEnhancers);

        endpoints.authenticationManager(authenticationManager)
                // 用户信息
                .userDetailsService(userDetailsService)
                // token转成JwtToken
                .tokenStore(jwtTokenStore)
                .accessTokenConverter(jwtAccessTokenConverter)
                .tokenEnhancer(chain);
                // // token存储到Redis
                // .tokenStore(redisTokenStore);
    }
}
